package com.apache.passport.service.impl;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.UctUserManager;

import java.util.List;
import java.util.Map;

/**
 * description:  用户信息表
 *
 * @author Hou Dayu 创建时间：2016-12-18
 */
public class UctUserManagerImpl implements UctUserManager {

    protected IDao uctUserDao;

    protected final String entityName = "com.apache.passport.entity.UctUser";

    protected Map<String, PluginConnector> plugins;

    public void setPlugins(Map<String, PluginConnector> plugins) {
        this.plugins = plugins;
    }

    public void setUctUserDao(IDao uctUserDao) {
        this.uctUserDao = uctUserDao;
    }

    public String saveInfo(ParamsVo<UctUser> vo) throws BusinessException {
        UctUser uctUser = vo.getObj();
        String infoId = Validator.generate();
        //定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
        String cacheKey = "";
        //String cacheKey="uctUser_"+infoId;
        uctUser.setUserId(infoId);
        MethodParam param = new MethodParam("UctUser", cacheKey, "", entityName);
        param.setVaule(uctUser);
        if (uctUserDao.insert(param)) {
            return infoId;
        }
        return "";
    }

    public boolean editInfo(ParamsVo<UctUser> vo) throws BusinessException {
        UctUser uctUser = vo.getObj();
        if (Validator.isNotNull(uctUser.getUserId())) {
            String cacheKey = "";
            //String cacheKey="uctUser_"+uctUser.getUserId();
            MethodParam param = new MethodParam("UctUser", cacheKey, "", entityName);
            param.setVaule(uctUser);
            return uctUserDao.edit(param);
        }
        return false;
    }

    public boolean deleteInfo(ParamsVo<UctUser> vo) throws BusinessException {
        String infoId = vo.getInfoId();
        if (Validator.isNull(infoId)) {
            return false;
        }
        String cacheKey = "";
        //String cacheKey="uctUser_"+infoId;
        String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        UctUser info = (UctUser) uctUserDao.selectById(param);
        if (Validator.isEmpty(info)) {
            return false;
        }
        param.setVaule(info);//此处需要先将状态值赋值为删除状态
        if ("false".equals(mark)) {//逻辑删除
            param.setKey("UctUser");
            return uctUserDao.edit(param);
        } else {
            param.setParams("userId", infoId);
            param.setDelete(true);
            return uctUserDao.delete(param);
        }
    }

    public Object getInfoById(ParamsVo<UctUser> vo) {
        String infoId = vo.getInfoId();
        //String cacheKey = "";
        String cacheKey = "uctUser_" + infoId;
        if (Validator.isNull(infoId)) {
            return null;
        }
        MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
        param.setInfoId(infoId);
        return uctUserDao.selectById(param);
    }

    public Object execute(ParamsVo<UctUser> vo) {
        String key = vo.getMethodKey();
        try {
            if (!Validator.isEmpty(plugins)) {
                if (!Validator.isEmpty(plugins.get(key))) {
                    return plugins.get(key).execute(vo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;
    }

    public Page getPageInfo(ParamsVo<UctUser> vo) {
        MethodParam param = setMethodParams(vo, 2);
        int pageSize = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
        int pageIndex = Integer
                .valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
        param.setPageIndex(pageIndex);
        param.setPageSize(pageSize);
        Page page = uctUserDao.pageSelect(param);
        return page;
    }

    public List<UctUser> getList(ParamsVo<UctUser> vo) {
        MethodParam param = setMethodParams(vo, 2);
        return uctUserDao.select(param);
    }

    public long countInfo(ParamsVo<UctUser> vo) {
        MethodParam param = setMethodParams(vo, 1);
        return uctUserDao.count(param);
    }

    private MethodParam setMethodParams(ParamsVo<UctUser> vo, int type) {
        String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
        StringBuilder sb = new StringBuilder(uctUserDao.getSql(type));
        MethodParam param = new MethodParam(methodKey, "", "", entityName);
        UctUser uctUser = vo.getObj();
        if (Validator.isNotNull(uctUser.getUserEname())) {
            sb.append(" and userEname = :userEname");
            param.setParams("userEname", uctUser.getUserEname());
        }
        if (Validator.isNotNull(uctUser.getUserCname())) {
            sb.append(" and userCname = :userCname");
            param.setParams("userCname", uctUser.getUserCname());
        }
        if (Validator.isNotNull(uctUser.getUserPass())) {
            sb.append(" and userPass = :userPass");
            param.setParams("userPass", uctUser.getUserPass());
        }
        if (Validator.isNotNull(uctUser.getEmail())) {
            sb.append(" and email = :email");
            param.setParams("email", uctUser.getEmail());
        }
        if (Validator.isNotNull(uctUser.getMobile())) {
            sb.append(" and mobile = :mobile");
            param.setParams("mobile", uctUser.getMobile());
        }

        if (Validator.isNotNull(uctUser.getRegIp())) {
            sb.append(" and regIp = :regIp");
            param.setParams("regIp", uctUser.getRegIp());
        }

        param.setSqlStr(sb.toString());
        return param;
    }
}
